<?php

/**
52zll.top
 */

if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

$lang = array_merge($lang, $scriptlang['dzapp_zhibo']);
require_once libfile('function/base', 'plugin/dzapp_base');
loadcache(array('','',''));
echo '<script type="text/javascript" src="static/js/calendar.js"></script>';

if(empty($_GET['ac'])) {

	if(!submitcheck('listsubmit')) {

		showformheader('plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_activity');
		showtableheader();
		showsubtitle(array('del', 'name', 'start_time', 'end_time', 'live_num', 'code_rate', 'activity_category', 'play_mode', 'push_streaming_server', ''));
		$perpage = 20;
		$start = ($page - 1) * $perpage;
		$mpurl = ADMINSCRIPT."?action=plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_activity";
		$param = array();
		$count = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->count_by_search($param);
		if($count) {
			$query = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->fetch_all_by_search($param, $start, $perpage);
			foreach($query as $value) {
				showtablerow('', array('class="td25"'), array(
					'<input type="checkbox" class="checkbox" name="delete[]" value="'.$value['activity_id'].'" />',
					$value['name'],
					$value['start_time'],
					$value['end_time'],
					$value['live_num'],
					$value['code_rate'],
					$value['activity_category'],
					$value['play_mode'],
					'rtmp://w.gslb.lecloud.com/live',
					'<a href="'.ADMINSCRIPT.'?action=plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_activity&ac=edit&id='.$value['activity_id'].'">'.cplang('edit').'</a>|<a href="http://live.lecloud.com/live/sdk/getView?activityId='.$value['activityid'].'" target="_blank">'.cplang('WEB_broadcast_address')'</a>|<a href="'.ADMINSCRIPT.'?action=plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_live&activity_id='.$value['activity_id'].'">'.cplang('other_taped_seats')'</a>|<a href="plugin.php?id=dzapp_zhibo&mod=view&activity_id='.$value['activity_id'].'" target="_blank">'.cplang('live_address')'</a>|<a href="'.ADMINSCRIPT.'?action=plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_graphic&activity_id='.$value['activity_id'].'">'.cplang('graphic_live')'</a>'
				));
			}
			$multipage = multi($count, $perpage, $page, $mpurl);
		}
		echo '<tr><td></td><td colspan="21"><div><a href="'.ADMINSCRIPT.'?action=plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_activity&ac=add" class="addtr">'.cplang('add_activity').'</a></div></td></tr>';
		showsubmit('listsubmit', 'submit', 'del', '', $multipage);
		showtablefooter();
		showformfooter();

	} else {

		if(is_array($_GET['delete'])) {
			C::t('#dzapp_zhibo#dzapp_zhibo_activity')->delete($_GET['delete']);
		}

		cpmsg('activity_update_succeed', 'action=plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_activity', 'succeed');
	}

} elseif($_GET['ac'] == 'add') {
	
	if(!submitcheck('addsubmit')) {

		showformheader('plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_activity&ac=add', 'enctype');
		showtableheader();
		showsetting('name', 'name', '', 'text');
		showsetting('start_time', 'start_time', '', 'calendar');
		showsetting('end_time', 'end_time', '', 'calendar');
		showsetting('cover_img_url', 'cover_img_url', '', 'filetext');
		showsetting('description', 'description', '', 'textarea');
		showsetting('live_num', 'live_num', '', 'text');
		showsetting('code_rate', array('code_rate', _select()), '', 'select');
		showsetting('need_record', 'need_record', '', 'radio');
		showsetting('need_time_shift', 'need_time_shift', '', 'radio');
		showsetting('need_full_view', 'need_full_view', '', 'radio');
		showsetting('activity_category', array('activity_category', _select_cate()), '', 'select');
		showsetting('play_mode', array('play_mode', _select_mode()), '', 'select');
		showsubmit('addsubmit', 'submit'); 
		showtablefooter();
		showformfooter();

	} else {

		if(!$_GET['name'] || !$_GET['start_time'] || !$_GET['end_time']) {
			cpmsg('activity_invalid', '', 'error');
		}
		
		if($cover_img_url = dzapp_pic_upload($_FILES['cover_img_url'], 'portal', 320, 240)) $_GET['cover_img_url'] = $cover_img_url['pic'];
		$data = array(
			'name' => $_GET['name'],
			'start_time' => $_GET['start_time'],
			'end_time' => $_GET['end_time'],
			'cover_img_url' => $_GET['cover_img_url'],
			'description' => $_GET['description'],
			'live_num' => $_GET['live_num'],
			'code_rate' => $_GET['code_rate'],
			'need_record' => $_GET['need_record'],
			'need_time_shift' => $_GET['need_time_shift'],
			'need_full_view' => $_GET['need_full_view'],
			'activity_category' => $_GET['activity_category'],
			'play_mode' => $_GET['play_mode'],
			'dateline' => $_G['timestamp'],
			'ip' => $_G['clientip'],
		);
		$activity_id = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->insert($data, true);
		require DISCUZ_ROOT.'./source/plugin/dzapp_zhibo/class/zhiboController.php';
		$zhibo = new zhiboController;
		$method_name = "letv.cloudlive.activity.create";
		$param = array(
			'activityName' => diconv($_GET['name'], CHARSET, 'UTF-8'),
			'startTime' => date('YmdHis', strtotime($_GET['start_time'])),
			'endTime' => date('YmdHis', strtotime($_GET['end_time'])),
			'coverImgUrl' => $_GET['cover_img_url'],
			'description' => diconv($_GET['description'], CHARSET, 'UTF-8'),
			'liveNum' => $_GET['live_num'],
			'codeRateTypes' => $_GET['code_rate'],
			'needRecord' => $_GET['need_record'],
			'needTimeShift' => $_GET['need_time_shift'],
			'needFullView' => $_GET['need_full_view'],
			'activityCategory' => $_GET['activity_category'],
			'playMode' => $_GET['play_mode'],
		);
		
		$rs = $zhibo->zhibo($method_name, $param);
		
		C::t('#dzapp_zhibo#dzapp_zhibo_activity')->update($activity_id, array('activityid' => $rs['activityId'])); 
		$param = array(
			'activityId' => $rs['activityId'],
			'neededPushAuth' => '1',
			'pushUrlValidTime' => '80000',
			'needIpWhiteList' => '0',
			'needPlayerDomainWhiteList' => '0',
		);
		$zhibo->zhibo('letv.cloudlive.activity.sercurity.config', $param);
		
		$param =  array(
			'activityId' => $rs['activityId'],
		);

		$rs = $zhibo->zhibo('letv.cloudlive.activity.getPushUrl', $param, 'get');
		
		foreach($rs['lives'] as $value){
			$param = array(
				'machine' => $value['machine'],
				'push_url' => str_replace('rtmp://w.gslb.lecloud.com/live/', '', $value['pushUrl']),
				'status' => $value['status'],
				'stream_id' => $value['streamId'],
				'activity_id' => $activity_id,
			);
			C::t('#dzapp_zhibo#dzapp_zhibo_live')->insert($param);
		}
		
		cpmsg('activity_add_succeed', 'action=plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_activity', 'succeed');
	}

} elseif($_GET['ac'] == 'edit') {

	$activity = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->fetch($_GET['id']);
	if(empty($activity)) {
		cpmsg('activity_nonexistence', '', 'error');
	}
	
	if(!submitcheck('editsubmit')) {

		showformheader('plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_activity&ac=edit&id='.$activity['activity_id'], 'enctype');
		showtableheader();
		showsetting('name', 'name', $activity['name'], 'text');
		showsetting('start_time', 'start_time', $activity['start_time'], 'calendar');
		showsetting('end_time', 'end_time', $activity['end_time'], 'calendar');
		showsetting('cover_img_url', 'cover_img_url', $activity['cover_img_url'], 'filetext');
		showsetting('description', 'description', $activity['description'], 'textarea');
		showsetting('live_num', 'live_num', $activity['live_num'], 'text');
		showsetting('code_rate', array('code_rate', _select()), $activity['code_rate'], 'select');
		showsetting('need_record', 'need_record', $activity['need_record'], 'radio');
		showsetting('need_time_shift', 'need_time_shift', $activity['need_time_shift'], 'radio');
		showsetting('need_full_view', 'need_full_view', $activity['need_full_view'], 'radio');
		showsetting('activity_category', array('activity_category', _select_cate()), $activity['activity_category'], 'select');
		showsetting('play_mode', array('play_mode', _select_mode()), $activity['play_mode'], 'select');
		showsubmit('editsubmit', 'submit'); 
		showtablefooter();
		showformfooter();

	} else {

		if(!$_GET['name'] || !$_GET['start_time'] || !$_GET['end_time']) {
			cpmsg('activity_invalid', '', 'error');
		}
		
		if($cover_img_url = dzapp_pic_upload($_FILES['cover_img_url'], 'portal', 320, 240)) $_GET['cover_img_url'] = $cover_img_url['pic'];
		if($cover_img_url && $activity['cover_img_url']) dzapp_pic_delete($activity['cover_img_url'], 'portal', 1);
		
		$data = array(
			'name' => $_GET['name'],
			'start_time' => $_GET['start_time'],
			'end_time' => $_GET['end_time'],
			'cover_img_url' => $_GET['cover_img_url'],
			'description' => $_GET['description'],
			'live_num' => $_GET['live_num'],
			'code_rate' => $_GET['code_rate'],
			'need_record' => $_GET['need_record'],
			'need_time_shift' => $_GET['need_time_shift'],
			'need_full_view' => $_GET['need_full_view'],
			'activity_category' => $_GET['activity_category'],
			'play_mode' => $_GET['play_mode'],
		);
		C::t('#dzapp_zhibo#dzapp_zhibo_activity')->update($activity['activity_id'], $data);

		cpmsg('activity_edit_succeed', 'action=plugins&operation=config&identifier=dzapp_zhibo&pmod=admincp_activity', 'succeed');
	}
}

function _select() {
	global $_G;
	$select[] = array(0, '');
	$select[] = array(10, 'fluent');
	$select[] = array(13, 'sdtv');
	$select[] = array(16, 'hdtv');
	$select[] = array(19, 'super_clear');
	$select[] = array(22, '720P');
	$select[] = array(25, '1080P');
	$select[] = array(99, 'painting');
	return $select;
}

function _select_cate() {
	global $_G;
	$select[] = array(0, '');
	$select[] = array('001', 'briefing');
	$select[] = array('002', 'wedding');
	$select[] = array('003', 'annual_meeting');
	$select[] = array('004', 'sports');
	$select[] = array('005', 'game');
	$select[] = array('006', 'travel_outdoor');
	$select[] = array('007', 'finance');
	$select[] = array('008', 'concert');
	$select[] = array('009', 'cooking');
	$select[] = array('010', 'pets_animals');
	$select[] = array('011', 'interview');
	$select[] = array('012', 'education');
	$select[] = array('013', 'athletic');
	$select[] = array('014', 'theatre');
	$select[] = array('015', 'evening_party');
	$select[] = array('016', 'tv_program');
	$select[] = array('017', 'show');
	$select[] = array('999', 'other');
	return $select;
}

function _select_mode() {
	global $_G;
	$select[] = array(0, 'realtime_zhibo');
	$select[] = array(0, 'delayed_zhibo');
	return $select;
}
//From:www_caogen8_co
?>